package cn.sun.plane.service.impl;

import cn.sun.plane.dao.impl.CsnDaoImpl;
import cn.sun.plane.entity.Csn;
import cn.sun.plane.entity.CsnEntity;
import cn.sun.plane.entity.PageBean;
import cn.sun.plane.service.CsnService;
import cn.sun.plane.util.ExcelUtil2;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
public class CsnServiceImpl implements CsnService {

    //注入管理的 DAO
    private CsnDaoImpl csnDao;

    public void setCsnDao(CsnDaoImpl csnDao) {
        this.csnDao = csnDao;
    }

    @Override
    public PageBean<CsnEntity> findByOther(Integer currPage, Csn csn) {

        PageBean<CsnEntity> pageBean = new PageBean<CsnEntity>();
        // 封装当前的页数
        pageBean.setCurrPage(currPage);
        //封装每页所显示的记录数
        int pageSize = 10;
        pageBean.setPageSize(pageSize);
        //封装总记录数
        int totalCount = csnDao.findCountByCsn(csn);
        pageBean.setTotalCount(totalCount);
        //封装总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);       //对double 向上取整
        pageBean.setTotalPage(num.intValue());
        //封装每页显示的数据
        int begin = (currPage - 1) * pageSize;
        List<CsnEntity> list = csnDao.findByOther(begin, pageSize,csn);

        pageBean.setList(list);
        return pageBean;
    }

//    下载方法
    @Override
    public ExcelUtil2 download(String title, Csn csn) {
//        得到要输出的数据
        List<CsnEntity> list = csnDao.findByOrderToDowmload(csn);
        List titles = new ArrayList<String>();
        titles.add(title);
//        要输出的列
        String[] rowsName = new String[] { "FD1","FD2","FD3","FD4","FD5","FD6","FD7","FD8","FD9","FD10",
                "FD11","FD12","FD13","FD14","FD15","FD16","FD17","FD19","FD20","FD21","FD22","FD23","FD24",
                "FD25","FD26","FD27","FD28","FD29","FD30"};

        List<Object[]> objects = new ArrayList<Object[]>();
        for(CsnEntity c : list){
            try {
//                Map map = BeanUtils.describe(c1);
//                objects.add(map.entrySet().toArray()) ;
//                objects.add(map.values().toArray()) ;
                Object[] o = {c.getFd1(),c.getFd2(),c.getFd3(),c.getFd4(),c.getFd5(),c.getFd6(),c.getFd7(),c.getFd8(),c.getFd9(),
                        c.getFd10(),c.getFd11(),c.getFd12(),c.getFd13(),c.getFd14(),c.getFd15(),c.getFd16(),c.getFd17(),
                        c.getFd19(),c.getFd20(),c.getFd21(),c.getFd22(),c.getFd23(),c.getFd24(),c.getFd25(),c.getFd26(),
                        c.getFd27(),c.getFd28(),c.getFd29(),c.getFd30()};
                objects.add(o);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
//        创建工具类实例，用于导出表格
        ExcelUtil2 ex = new ExcelUtil2(titles, rowsName, objects);
        return ex;
    }

    @Override
    // 分页查询的方法
    public PageBean<CsnEntity> findByPage(Integer currPage) {
        PageBean<CsnEntity> pageBean = new PageBean<CsnEntity>();
        // 封装当前的页数
        pageBean.setCurrPage(currPage);
        //封装每页所显示的记录数
        int pageSize = 10;
        pageBean.setPageSize(pageSize);
        //封装总记录数
        int totalCount = csnDao.findCount();
        pageBean.setTotalCount(totalCount);
        //封装总页数
        double tc = totalCount;
        Double num = Math.ceil(tc / pageSize);       //对double 向上取整
        pageBean.setTotalPage(num.intValue());
        //封装每页显示的数据
        int begin = (currPage - 1) * pageSize;
        List<CsnEntity> list = csnDao.findByPage(begin, pageSize);

        pageBean.setList(list);
        return pageBean;
    }

    @Override
    //业务层保存的方法
    public void save(CsnEntity csn) {
        csnDao.save(csn);
    }

    @Override
    public CsnEntity findById(String did) {
        return csnDao.findById(did);
    }

    @Override
    public void update(CsnEntity csn) {
        csnDao.update(csn);
    }

    @Override
    public void delete(CsnEntity csn) {
        csnDao.delete(csn);
    }

    @Override
    //查询所有的方法
    public List<CsnEntity> findAll() {
        return csnDao.findAll();
    }
}
